package com.huel.liweijian.Controller;

import com.huel.liweijian.Service.CalculatorFactory;
import com.huel.liweijian.Service.CalculatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Liweijian.
 * @date 2024/9/4 10:47
 */
@RestController
@RequestMapping("/calculator")
public class CalculatorController {

    @Autowired
    private CalculatorFactory calculatorFactory;

    @GetMapping("/calculate")
    public double calculate(@RequestParam String operation,
                            @RequestParam double a,
                            @RequestParam(required = false) Double b) {
        CalculatorService calculatorService = calculatorFactory.getOperation(operation);
        return calculatorService.calculate(a, b);
    }
}
